module.exports = function(grunt) {
    grunt.file.defaultEncoding = 'utf-8';
    /**project configuration*/
    grunt.initConfig({
        pkg : grunt.file.readJSON('package.json'),

        clean: {
            contents: ['dest/*'],
            subfolders: ['dest/*/'],
            css: ['dest/assets/css/*'],
            js: ['dest/assets/js/*'],
            cleantemp:['dest/assets/css/temp']
        },
        less: {
            default: {
                files: [{
                    expand: true,
                    cwd: 'src/less',
                    src: ['*.less'],
                    dest: 'dest/assets/css/temp',
                    ext: '.css'
                }]
            }
        },
        cssmin: {
            default:{
                files: [{
                    expand: true,
                    cwd: 'dest/assets/css/temp',
                    src: ['*.css'],
                    dest: 'dest/assets/css/',
                    ext: '.min.css'
                }]
            }
        },
        jshint:{
            options:{
                expr:true, 
                lastsemic : true,
                scripturl:true, 
                ignores : []
            },
            beforeuglify:['Gruntfile.js', 'src/js/*.js', '!src/js/Markdown.*.js'],
            afteruglify:['!dest/assets/js/common.min.js']
        },
        uglify:{
            buildall: {
                files: [{
                    expand:true,
                    cwd: 'src/js',
                    ignores:['Markdown.*.js'],
                    src:['*.js', '!Markdown.*.js'],//所有js文件
                    dest: 'dest/assets/js',//输出到此目录下
                    ext: '.min.js'
                }]
            },
        },
        copy: {
            main: {
                expand: true,
                nonull: true,
                cwd: 'src/html',
                src: '**',
                dest: 'dest/',
                options: {
                }
            },
            resources: {
                expand: true,
                nonull: true,
                cwd: 'src/images',
                src: '**',
                dest: 'dest/assets/images',
            },
            special: {
                expand: true,
                nonull: true,
                cwd: 'src/js',
                src: ['Markdown.*.js'],
                dest: 'dest/assets/js',
            }
        },
        watch: {
            css: {
                files: ['src/less/*.less'],
                tasks: ['less', 'cssmin', 'clean:cleantemp'],
                options: {
                    interrupt: true,
                    spawn: false
                }
            },
            js: {
                files: ['src/js/*.js'],
                tasks: ['jshint:beforeuglify', 'uglify', 'jshint:afteruglify'],
                options: {
                    interrupt: true,
                    spawn: false
                }
            },
            resources: {
                files: ['src/html/*', 'src/html/**/*', 'src/images/*', 'src/images/**/*'],
                tasks: ['copy'],
                options: {
                    interrupt: true,
                    spawn: false,
                    event: ['all'],
                }
            }
        }
    });

    /**load tasks package*/
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-jshint');/**语法检查**/
    grunt.loadNpmTasks('grunt-contrib-concat');/**js 合并**/
    grunt.loadNpmTasks('grunt-contrib-uglify');/**js 压缩**/
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-watch');
    
    grunt.registerTask('default', ['clean', 'less', 'cssmin', 'jshint:beforeuglify', 'uglify', 'jshint:afteruglify', 'clean:cleantemp', 'copy', 'watch']);
};